package org.fao.pims.persistence;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.sql.*;
import javax.sql.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class BaseDao {
	
	private static Log log = LogFactory.getLog(BaseDao.class);
	
	private DataSource datasource = null;
	
	
	public BaseDao() {
		
		datasource = new MyBasicDataSource().setup();
		
	}
	
	/**
     * Preleva i dati del result set contenuti in un oggetto List e li salva in un 
     * bean appropriato, facendo corrispondere un bean a ciascun record.
     * Restituisce una lista di bean
     */
    protected abstract List makeBeanList(List rows);
	
	/**
     * The methos copies the result set in an object List. Each object in the list holds 
     * a table field name, type and value.
     */
    protected List resultsetToList(ResultSet rs) throws SQLException
    {
        int rowsCount = 0;
        ResultSetMetaData rsmd = null;
        List row = new ArrayList();
        
        rsmd = rs.getMetaData();
        
        String tableName = rsmd.getTableName(1);
        
        int cols = rsmd.getColumnCount();
            
            while(rs.next()){
                
                rowsCount++;

                List fields = new ArrayList();
                for(int i = 0; i < cols; i++){
                	
                    String columnClassName = rsmd.getColumnClassName(i + 1);
                    
                    String columnName = rsmd.getColumnName(i + 1);
                    
                    //System.out.println("Column name: " + columnName + ", column data type: " + columnClassName + " ");
                    
                    TableFieldHandler fieldhandler;
                    
                   
                    if( !columnClassName.equals("java.lang.Object") )
                    {
                        //not binary data types
                    	if("[B".endsWith(columnClassName)) {
                    		//this handle mysql text data type that return as a wrong type
                    		String mysqlTextDataType = rs.getString(i + 1);
                    		columnClassName = "java.lang.String";
                    		fieldhandler = new TableFieldHandler(columnClassName, mysqlTextDataType, columnName);
                    	}
                    	else {
                    		
                    		Object object = rs.getObject(i + 1);
                    		
                    		fieldhandler = new TableFieldHandler(columnClassName, object, columnName);
                    	}
                         
                    }
                    else 
                    {
                    	// binary data types
                        java.sql.Blob blob = rs.getBlob(i + 1);
                                                                     
                        InputStream blobstream = blob.getBinaryStream();
                        fieldhandler = new TableFieldHandler(columnClassName,
                                                                               blobstream, 
                                                                               columnName);
                    }
                    
                    fields.add(fieldhandler);
                    
                }
                
                row.add(fields);
                
            }
            
        
        return row;
    }


	public DataSource getDatasource() {
		return datasource;
	}

	
}
